package simple.engine.drawable;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import simple.engine.objects.Texture2D;
import simple.engine.objects.Vector4f;
import android.graphics.Matrix;

public class SimpleObject {
	
	private String id;
	private Vector4f mColor = new Vector4f();
	
	private float mVertices[];
	private int mVerticesCount;
	private FloatBuffer mVertexBuffer;
	
	private Texture2D mTexture;

	public SimpleObject(String id, float x, float y, float width, float height)
	{
		this.id = id; 
		
		mVertices = new float[]{
			x, y - height, 0.0f,					// V1 - bottom left
			x, y, 0.0f,					// V2 - top left
			x + width, y - height,  0.0f,		// V3 - bottom right
			x + width, y,  0.0f			// V4 - top right
		};
		mVerticesCount = mVertices.length / 3;
		
		// Allocate buffer
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(mVertices.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		mVertexBuffer = byteBuffer.asFloatBuffer();
		mVertexBuffer.put(mVertices);
		mVertexBuffer.position(0);
	}
	
	// Translations
	
	public void rotateAtPoint(float x, float y, float degrees)
	{
		Matrix matrix = new Matrix();
		matrix.postRotate(degrees, x, y);
		matrix.mapPoints(mVertices);
		
		updateBuffer();
	}
	
	public void move(float x, float y)
	{
		mVertices[0] += x;
		mVertices[3] += x;
		mVertices[6] += x;
		mVertices[9] += x;
		
		mVertices[1] += y;
		mVertices[4] += y;
		mVertices[7] += y;
		mVertices[10] += y;
		
		updateBuffer();
	}
	
	private void updateBuffer()
	{
		mVertexBuffer.clear();
		mVertexBuffer.put(mVertices);
		mVertexBuffer.position(0);
	}
	
	// Setters and getters (equivalent)
	
	public String getID()
	{
		return this.id;
	}
	
	public void setTexture(Texture2D texture)
	{
		mTexture = texture;
	}
	
	public void setColor(float r, float g, float b, float alpha)
	{
		mColor.setValue(r, g, b, alpha);
	}
	
	// Drawing
	
	public void draw(GL10 gl)
	{
		gl.glPushMatrix();
		
		// Use alpha bits for transparency
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glEnable(GL10.GL_BLEND);    
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		gl.glEnable(GL10.GL_ALPHA_BITS);
		
		// Set the colour for the square
		gl.glColor4f(mColor.getX(), mColor.getY(), mColor.getZ(), mColor.getAlpha());
		
		// Bind the previously generated texture
		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexture.getTexturePointer());
		
		// Point to our buffers
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

		// Set the face rotation
		gl.glFrontFace(GL10.GL_CW);
		
		// Point to our vertex buffer
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexture.getTextureBuffer());

		// Draw the vertices as triangle strip
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, mVerticesCount);

		//Disable the client state before leaving
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		
		gl.glDisable(GL10.GL_ALPHA_BITS);
		gl.glDisable(GL10.GL_TEXTURE_2D);
		
		gl.glPopMatrix();
	}
	
	// Overrides
	
	@Override
	public String toString() {
		return "";
	}
	
}
